package com.baichen.agent.tools;

import com.baichen.agent.entity.Appointment;
import com.baichen.agent.service.AppointmentService;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AppointmentTools {

    @Autowired
    private AppointmentService appointmentService;

    @Tool(name = "查询是否有号源", value="根据科室名称，日期，时间和医生查询是否有号源，并返回给用户")
    public boolean checkDepartment(
            @P(value = "科室名称") String department,
            @P(value = "日期") String date,
            @P(value = "时间，可选值：上午、下午", required = false) String time,
            @P(value = "医生名称") String doctorName) {
        System.out.println("checkDepartment called with department: " + department + ", date: " + date + ", time: " + time + ", doctorName: " + doctorName);
        return true;
    }

    @Tool(name = "预约挂号", value = "根据参数，先执行工具方法queryDepartment查询是否可预约，并直接给用户回答是否可预约，并让用户确认所有预约信息，用户确认后再进行预约。如果用户没有提供具体的医生姓名，请从向量存储中找到一位医生")
    public String bookAppointment(Appointment appointment) {
        Appointment exist = appointmentService.exist(appointment);
        if (exist == null) {
            appointment.setId(null);//防止大模型幻觉设置了id
            appointmentService.save(appointment);
            return "预约成功";
        } else {
            return "您已经预约过该时间段的号源，请勿重复预约";
        }
    }

    @Tool(name = "取消预约", value = "根据参数，查询预约是否存在，如果存在则删除预约记录并返回取消预约成功，否则返回取消预约失败")
    public String cancelAppointment(Appointment appointment) {
        Appointment exist = appointmentService.exist(appointment);
        if (exist != null) {
            appointmentService.removeById(exist.getId());
            return "取消预约成功";
        } else {
            return "取消预约失败";
        }
    }
}
